﻿namespace ServerApp.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Server;

    public class CustomerService
    {

        /// <summary>
        /// Convert entety Customer class to model class
        /// </summary>
        /// <param name="customer">Entety Customer class</param>
        /// <returns>User</returns>
        private static User entetyToModel(Customer customer)
        {
            User result = new User(customer.CustomerId)
            {
                Email = customer.Email,
                FIO = customer.FIO,
                Login = customer.Login,
                Password = String.Empty,
                Phone = customer.Phone,
                PlaceId = customer.FK_PlaceId,
                Role = (UserRole)customer.Role,
            };

            return result;
        }

        /// <summary>
        /// Add user to datastore
        /// </summary>
        /// <param name="user">user for add</param>
        /// <returns>User</returns>
        public static User Add(User user)
        {
            User result = User.Null;

            InternetShopBaseEntities efmodel = SingleEntity.GetInstance();

            List<Customer> customers = (from info in efmodel.Customers
                                        where info.Login == user.Login && info.Password == user.Password
                                        select info).ToList();

            if (customers.Count == 0)
            {

                Customer customer = efmodel.Customers.CreateObject();

                customer.Email = user.Email;
                customer.FIO = user.FIO;
                customer.FK_PlaceId = (user.PlaceId != 0) ? user.PlaceId : null;
                customer.Login = user.Login;
                customer.Password = user.Password;
                customer.Phone = user.Phone;
                customer.Role = (byte)user.Role;

                efmodel.AddToCustomers(customer);
                efmodel.SaveChanges();

                result = entetyToModel(customer);
            }

            return result;  
        }

        /// <summary>
        /// Update user
        /// </summary>
        /// <param name="user">user</param>
        public static User Update(User user)
        {
            InternetShopBaseEntities efmodel = SingleEntity.GetInstance();

            List<Customer> customers = (from info in efmodel.Customers
                           where info.CustomerId == user.Id
                           select info).ToList();

            Customer customer;

            if (customers.Count() != 0)
            {
                customer = customers.First();

                customer.Email = user.Email;
                customer.FIO = user.FIO;
                customer.FK_PlaceId = user.PlaceId;
                customer.Login = user.Login;

                if (!string.IsNullOrEmpty(user.Password))
                {
                    customer.Password = user.Password;
                }

                customer.Phone = user.Phone;
                customer.Role = (byte)user.Role;
                efmodel.SaveChanges();
            }
            else
            {
                throw new IndexOutOfRangeException();
            }

            return entetyToModel(customer);  
        }

        /// <summary>
        /// Delete user
        /// </summary>
        /// <param name="user">delete user</param>
        public static void Delete(User user)
        {
            user.Role = UserRole.Banned;
            Update(user);
        }

        /// <summary>
        /// Get user by login+password
        /// </summary>
        /// <param name="login">Login</param>
        /// <param name="password">Password</param>
        /// <returns>User data, or null if exception</returns>
        public static User GetUser(string login, string password, int? placeId)
        {
            User result = User.Null;

            InternetShopBaseEntities model = SingleEntity.GetInstance();

            List<Customer> customers = (from info in model.Customers
                                        where info.Login == login && info.Password == password
                                        select info).ToList();

            if (customers.Count() != 0)
            {
                Customer customer = customers.First();
                result = new User(customer.CustomerId)
                {
                    Email = customer.Email,
                    FIO = customer.FIO,
                    Login = customer.Login,
                    Password = String.Empty,
                    Phone = customer.Phone,
                    PlaceId = customer.FK_PlaceId,
                    Role = (UserRole)customer.Role,
                };
            }

            return result;
        }

        public static User GetUser(long id)
        {
            User result = User.Null;

            InternetShopBaseEntities model = SingleEntity.GetInstance();

            List<Customer> customers = (from info in model.Customers
                                        where info.CustomerId == id
                                        select info).ToList();

            if (customers.Count() != 0)
            {
                Customer customer = customers.First();
                result = new User(customer.CustomerId)
                             {
                                 Email = customer.Email,
                                 FIO = customer.FIO,
                                 Login = customer.Login,
                                 Password = String.Empty,
                                 Phone = customer.Phone,
                                 PlaceId = customer.FK_PlaceId,
                                 Role = (UserRole)customer.Role,
                             };
            }

            return result;
        }

        /// <summary>
        /// Get users list
        /// </summary>
        /// <param name="filter">Filter dictionary</param>
        /// <returns>List of users or null if exception</returns>
        public static Dictionary<long, User> GetList(Dictionary<string, object> filter = null, int page = 0)
        {
            Dictionary<long, User> result = new Dictionary<long, User>();

            InternetShopBaseEntities model = SingleEntity.GetInstance();

            List<Customer> customers = model.Customers.ToList();

            if (filter == null)
            {
                filter = new Dictionary<string, object>();
            }

            if (filter.ContainsKey("place"))
            {
                customers = (from info in customers
                             where info.FK_PlaceId == (int)filter["place"]
                                            select info).ToList();
            }

            foreach (Customer customer in customers)
            {
                User user = new User(customer.CustomerId)
                {
                    Email = customer.Email,
                    FIO = customer.FIO,
                    Login = customer.Login,
                    Password = String.Empty,
                    Phone = customer.Phone,
                    PlaceId = customer.FK_PlaceId,
                    Role = (UserRole)customer.Role,
                };

                result.Add(customer.CustomerId, user);
            }

            return result;
        }
    }
}
